package com.gdk.ksoft.ui.sales;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.entry.tables.SaleBuySelfTable;
import com.gdk.ksoft.ui.sales.adapters.GiveSelfTypeAdapter;
import com.gdk.ksoft.assembly.utils.system.SQLiteUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnItemClick;

import java.util.List;

/***
 * 选择买赠本品方案页面
 */
public class GiveSelfTypeActivity extends Activity {


    /**
     * 添加买赠本品的请求码
     */
    public static final int REQ_CODE_FOR_DETAIL = 0x0001;

    @ViewInject(R.id.pgiveselfType_listType)private ListView listType;//方案列表

    private int kh_id,shdz_id;//客户ID，送货地址ID
    private String priceName;//客户价格名称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_give_self_type);
        ViewUtils.inject(this);
        Intent intent = getIntent();
        if(intent!=null){
            kh_id = intent.getIntExtra("netCustomerId",0);
            shdz_id = intent.getIntExtra("netAddressId",0);
            priceName = intent.getStringExtra("priceName");
        }
        findAllSaleSelfType(this);//查询所有促销本品方案
    }

    /**
     * 商品类型项点击事件
     * @param av
     * @param view
     * @param position
     * @param itemId
     */
    @OnItemClick(value={R.id.pgiveselfType_listType})
    public void onTypeItemClick(AdapterView<?> av,View view,int position,long itemId){
        final SaleBuySelfTable item = (SaleBuySelfTable)listType.getItemAtPosition(position);
        Intent intent = new Intent(this,GiveSelfActivity.class);
        intent.putExtra("cx_id",item.id);
        intent.putExtra("kh_id",kh_id);
        intent.putExtra("shdz_id",shdz_id);
        startActivityForResult(intent, REQ_CODE_FOR_DETAIL);
        overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
    }

    /**
     * 查询所有促销本品方案
     */
    public void findAllSaleSelfType(final Context context){
        new AsyncTask<Void,Void,Void>(){
            private String errMessage = null;
            private ProgressDialog dialog = null;
            private List<SaleBuySelfTable> list = null;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog = ProgressDialog.show(context,"","正在加载中...");
            }

            @Override
            protected Void doInBackground(Void... params) {
                try{
                    DbUtils dbUtils = SQLiteUtils.getInstance(context).getDbUtils();
                    list = dbUtils.findAll(SaleBuySelfTable.class);
                }catch (Exception e){
                    e.printStackTrace();
                    errMessage = e.getMessage();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                if(dialog!=null && dialog.isShowing()) dialog.dismiss();
                if(errMessage!=null){
                    Toast.makeText(context,"加载失败!原因："+errMessage,Toast.LENGTH_SHORT).show();
                    return;
                }
                if(list == null || list.size()<=0){
                    Toast.makeText(context,"当前没有任何方案!",Toast.LENGTH_SHORT).show();
                    return;
                }
                listType.setAdapter(new GiveSelfTypeAdapter(context,list));
            }
        }.execute();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQ_CODE_FOR_DETAIL && resultCode == Activity.RESULT_OK){
            this.finish();//关闭该页面
        }
    }

    /**
     * 页面返回
     */
    public void goBack() {
        this.finish();
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }

    @OnClick(value = {R.id.pgiveselfType_imgBack})
    public void OnBackClick(View view) {
        goBack();//页面返回
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK){
            goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
